package oocl.ita.eordering.controller;

import java.util.Date;
import java.util.List;

import javax.jms.JMSException;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import oocl.ita.eordering.po.Client;
import oocl.ita.eordering.po.Merchant;
import oocl.ita.eordering.po.Order;
import oocl.ita.eordering.senJms.ClientSendJms;
import oocl.ita.eordering.service.MerchantService;
import oocl.ita.eordering.service.OrderService;
import oocl.ita.eordering.util.GradeCalculate;
import oocl.ita.eordering.vo.Result;
import oocl.ita.eordering.vo.Vorder;

@Controller
@RequestMapping("/action")
public class OrderController {
	@Autowired(required = true)
	private OrderService orderService;
	@Autowired
	private ClientSendJms clientSendJms;
	@Autowired(required = true)
	private MerchantService merchantService;
	@Autowired
	private GradeCalculate gradeCaculate;

	// 完成订单
	@RequestMapping("/confirmOrder")
	@ResponseBody
	public Result confirmOrder(Integer oId, Double mStark) throws JMSException {
		Result result = new Result();
		Order confirmOrder = new Order();
		confirmOrder.setoId(oId);
		confirmOrder.setoStatus(3);
		confirmOrder.setFinishTime(new Date());
		confirmOrder.setoStark(mStark);

		if (oId != null && mStark >= 0 && mStark <= 10) {
			Order order = orderService.showOrderByOrderId(oId);
			Merchant merchant = new Merchant();
			merchant.setmId(order.getmId());
			order.setoStark(mStark);
			if (gradeCaculate.gradeCalculatebyM(merchant, order) <= 5) {
				// ***add star logic
				int returnValue = clientSendJms.CSendJms(merchant);// 发送jms到A端
				if (returnValue == 1) {
					System.out.println("score has sent to admin successfully");
					result.success("感谢评分");
				} else {
					result.fail("请重试");
				}
			}
			result.success("感谢评分");
			orderService.updateOrder(confirmOrder);
		} else {
			result.fail("系统错误，请重试");
		}
		return result;
	}

	// 发投诉订单的oId去A端
	@RequestMapping("/complaintOrder")
	@ResponseBody
	public Result complaintOrderId2Admin(String complaint, Integer oId) throws JMSException {
		int mId = orderService.complaintOrder(complaint, oId);
		Merchant merchant = merchantService.findMerchantById(mId);
		String merchantBrand = merchant.getmBrand();
		Order order=orderService.showOrderByOrderId(oId);
		order.setComplaint(complaint);
		orderService.updateOrder(order);
		Result result = new Result();
		int resultValue = clientSendJms.CSendJms(oId, merchantBrand);
		if (resultValue == 1) {
			result.success("投诉成功");
		} else {
			result.fail("投诉失败，请重试");
		}
		return result;
	}

	// 查看所有投诉订单
	@RequestMapping("/showComplaintOrder")
	public String updateOrder(Order order, HttpSession session) {
		Client client = (Client) session.getAttribute("c");
//		List<Vorder> complaintorders = orderService.findComplaintOrders(client.getcId());
		//以下代码与showOrder一样，后期解决
		List<Vorder> complaintorders = orderService.showOrderByClientId(client.getcId());
		List<Merchant> merchants = merchantService.findAllMerchant();
		if (complaintorders != null) {
			for (Vorder each : complaintorders) {
				System.out.println(each .getoId());
				for (Merchant merchant : merchants) {
					if (merchant.getmId().equals(each .getmId())) {
						each.setMerchant_name(merchant.getmBrand());
					}
				}
			}
		}
		session.setAttribute("complaintorders", complaintorders);
		return "evaluations";
	}

	//查看
}
